package com.september.mongo.data;


import java.net.UnknownHostException;
import java.util.Properties;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.logging.MorphiaLoggerFactory;
import com.google.code.morphia.logging.slf4j.SLF4JLogrImplFactory;
import com.mongodb.Mongo;
import com.september.prop.Properteer;

public class DataProvider {

	private static Mongo mongo;
	private static Morphia morphia;
	private static Datastore ds;
	//private static DataProvider dataProvider = null;
	
	static {
		//MorphiaLoggerFactory.registerLogger(SLF4JLogrImplFactory.class);
		try {
			Properties prop = Properteer.getInstance().Properties();
			MorphiaLoggerFactory.registerLogger(SLF4JLogrImplFactory.class); 
			mongo = new Mongo(prop.getProperty("DB_HOST"), Integer.parseInt(prop.getProperty("DB_PORT")));
			morphia = new Morphia();
			morphia.mapPackage(prop.getProperty("MAPPING_PACKAGE"));
			ds = morphia.createDatastore(mongo, prop.getProperty("DB_NAME"), prop.getProperty("DB_USER"), prop.getProperty("DB_PASSWORD").toCharArray());
			ds.ensureIndexes();
			
			Properteer.getInstance().getLogger(DataProvider.class).debug("DataProvider correctly initialized");
		} catch (UnknownHostException e) {
			Properteer.getInstance().getLogger(DataProvider.class).error(e.getClass().getName(), e);
		}
		
	}

	public DataProvider(){
		
	}
	
	
	public Datastore getDatastore() {
		return ds;
	}

}
